unit untAbsEdicao;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.ToolWin, Vcl.ExtCtrls,
  Vcl.AppEvnts, Vcl.ImgList, Vcl.ActnList, ADODB, DB, DBCtrls, Vcl.StdCtrls,
  Vcl.Mask, ClassBase;

type
  TfrmAbsEdicao = class(TForm)
    acBd: TActionList;
    acAlterar: TAction;
    acCancelar: TAction;
    acSair: TAction;
    acGravar: TAction;
    imgAbs16: TImageList;
    imgAbs24: TImageList;
    Panel1: TPanel;
    pnBotoes: TPanel;
    toolBarPrincipal: TToolBar;
    ToolButton9: TToolButton;
    tbConsultar: TToolButton;
    tbPesquisa: TToolButton;
    ToolButton3: TToolButton;
    ToolButton5: TToolButton;
    ToolButton7: TToolButton;
    ToolButton4: TToolButton;
    ToolButton6: TToolButton;
    tbSair: TToolButton;
    ToolButton2: TToolButton;
    Panel3: TPanel;
    BalloonHint1: TBalloonHint;
    procedure acSairExecute(Sender: TObject);
    procedure acCancelarExecute(Sender: TObject);
    procedure acGravarExecute(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure acAlterarExecute(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    FBrowse : Boolean;
    procedure SetaFocu;
  protected
    FBase : TBase;
    function cdsAtual : TADOQuery;
    procedure AtualizarBotoes;
    procedure CaixaAlta; virtual;
    procedure AtualizaLookUps; virtual;
    procedure FechaLookUps; virtual;
  public
    { Public declarations }
    constructor Create(AOwner : TComponent); Overload; virtual;
    constructor Create(AOwner : TComponent; ID : Integer); Overload; virtual;
  end;

var
  frmAbsEdicao: TfrmAbsEdicao;

implementation

{$R *.dfm}



{ TfrmAbsEdicao }

procedure TfrmAbsEdicao.acAlterarExecute(Sender: TObject);
begin
  cdsAtual.Edit();
  SetaFocu;
end;

procedure TfrmAbsEdicao.acCancelarExecute(Sender: TObject);
begin
  cdsAtual.Cancel();
end;

procedure TfrmAbsEdicao.acGravarExecute(Sender: TObject);
begin
  cdsAtual.UpdateRecord;

  if FBase.ValidaCamposRequeridos(cdsAtual)then
  begin
    cdsAtual.Post();
    ActiveControl:= nil;
    AtualizarBotoes();
  end;
end;

procedure TfrmAbsEdicao.acSairExecute(Sender: TObject);
begin
  Close();
end;

procedure TfrmAbsEdicao.AtualizaLookUps;
var
  i : Integer;
begin
  for i := 0 to Self.ComponentCount -1 do
  if (Components[i] is TDBLookupComboBox ) then
    if TDBLookupComboBox(Components[i]).Tag = 0 then
    begin
       TDBLookupComboBox(Components[i]).ListSource.DataSet.Close();
       TDBLookupComboBox(Components[i]).ListSource.DataSet.Open();
    end;
end;

procedure TfrmAbsEdicao.AtualizarBotoes;
begin
  if cdsAtual.ControlsDisabled then
    Exit;

  FBrowse := (cdsAtual.State = dsBrowse);

  acGravar.Enabled    := cdsAtual.State in [dsEdit, dsInsert];
  acCancelar.Enabled  := acGravar.Enabled;
  acSair.Enabled      := FBrowse;
  acAlterar.Enabled   := FBrowse and (cdsAtual.RecordCount > 0);
//  AplicaPermissoes();
end;

procedure TfrmAbsEdicao.CaixaAlta;
var
  i : Integer;
begin
  for i := 0 to ComponentCount - 1 do
  begin
    if (Components[i] is TDBEdit) then
    begin
      TDBEdit(Components[i]).CharCase := ecUpperCase;
    end;
  end;
end;

function TfrmAbsEdicao.cdsAtual: TADOQuery;
var
  i : Integer;
begin
  Result := nil;

  for i := 0 to ComponentCount - 1 do
  if (Components[i] is TADOQuery) then
    if TADOQuery(Components[i]).Tag = 1 then
      Result := TADOQuery(Components[i]);
end;

constructor TfrmAbsEdicao.Create(AOwner: TComponent; ID: Integer);
begin
  inherited
  Create(AOwner);
  cdsAtual.Close();
  cdsAtual.SQL.Add(' WHERE ID = :ID');
  cdsAtual.Parameters.ParamByName('ID').Value := ID;
  cdsAtual.Open();
end;

constructor TfrmAbsEdicao.Create(AOwner: TComponent);
begin
  inherited
  Create(AOwner);
  cdsAtual.Close();
  cdsAtual.SQL.Add(' WHERE 1 = 2');
  cdsAtual.Open();
  cdsAtual.Insert();
end;

procedure TfrmAbsEdicao.FechaLookUps;
var
  i : Integer;
begin
  for i := 0 to Self.ComponentCount -1 do
    if (Components[i] is TDBLookupComboBox ) then
      TDBLookupComboBox(Components[i]).ListSource.DataSet.Close();
end;

procedure TfrmAbsEdicao.FormActivate(Sender: TObject);
begin
  AtualizaLookUps();
end;

procedure TfrmAbsEdicao.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  FechaLookUps();
end;

procedure TfrmAbsEdicao.FormShow(Sender: TObject);
begin
  FBase := TBase.Create;
  SetaFocu;
  CaixaAlta();
end;

procedure TfrmAbsEdicao.SetaFocu;
var
  i : Integer;
begin
  if cdsAtual.State = dsInsert then
  begin
    for i := 0 to ComponentCount - 1 do
      if (Components[i] is TWinControl) and
         (Components[i].Tag = 1) then
      begin
        TWinControl(Components[i]).SetFocus;
        Exit;
      end;
  end;
end;

end.
